package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class LevelTimer 
	{
		public var clockFaceSprite:FlxSprite;
		public var nextClockFaceSprite:FlxSprite;
		public var timerText:FlxText;
		public var time:Number;
		public var visible:Boolean = true;
		
		private var _levelTimer:FlxTimer;
		public var clockHandSprite:FlxSprite;
		private var _highestTimeValue:Number;
		
		private var _pauseText:FlxText;
		
		public function LevelTimer(duration:Number, repeat:int = 0) 
		{
			clockFaceSprite = new FlxSprite(0, 0);
			clockFaceSprite.loadGraphic(ImageFiles.clockFace, true, false, 45, 45);
			clockFaceSprite.x = FlxG.width - clockFaceSprite.width;
			clockFaceSprite.scrollFactor.x = clockFaceSprite.scrollFactor.y = 0;
			clockFaceSprite.addAnimation("zero", [0]);
			clockFaceSprite.addAnimation("one", [1]);
			clockFaceSprite.addAnimation("two", [2]);
			clockFaceSprite.addAnimation("three", [3]);
			clockFaceSprite.addAnimation("four", [4]);
			clockFaceSprite.addAnimation("five", [5]);
			clockFaceSprite.addAnimation("six", [6]);
			clockFaceSprite.addAnimation("seven", [7]);
			clockFaceSprite.addAnimation("eight", [8]);
			clockFaceSprite.addAnimation("nine", [9]);
			clockFaceSprite.addAnimation("ten", [10]);
			clockFaceSprite.addAnimation("eleven", [11]);
			clockFaceSprite.addAnimation("twelve", [12]);
			
			nextClockFaceSprite = new FlxSprite(0, 0);
			nextClockFaceSprite.loadGraphic(ImageFiles.clockFace, true, false, 45, 45);
			nextClockFaceSprite.x = FlxG.width - clockFaceSprite.width;
			nextClockFaceSprite.scrollFactor.x = nextClockFaceSprite.scrollFactor.y = 0;
			nextClockFaceSprite.addAnimation("zero", [0]);
			nextClockFaceSprite.addAnimation("one", [1]);
			nextClockFaceSprite.addAnimation("two", [2]);
			nextClockFaceSprite.addAnimation("three", [3]);
			nextClockFaceSprite.addAnimation("four", [4]);
			nextClockFaceSprite.addAnimation("five", [5]);
			nextClockFaceSprite.addAnimation("six", [6]);
			nextClockFaceSprite.addAnimation("seven", [7]);
			nextClockFaceSprite.addAnimation("eight", [8]);
			nextClockFaceSprite.addAnimation("nine", [9]);
			nextClockFaceSprite.addAnimation("ten", [10]);
			nextClockFaceSprite.addAnimation("eleven", [11]);
			nextClockFaceSprite.addAnimation("twelve", [12]);
			nextClockFaceSprite.alpha = 0;
			
			clockHandSprite = new FlxSprite(0, 0);
			clockHandSprite.loadGraphic(ImageFiles.clockHand, false, false);
			clockHandSprite.origin = new FlxPoint (1, 22);
			clockHandSprite.x = clockFaceSprite.x + clockFaceSprite.width / 2 - clockHandSprite.width / 2;
			clockHandSprite.y = clockFaceSprite.y + clockFaceSprite.height / 2 - clockHandSprite.origin.y;
			clockHandSprite.scrollFactor.x = clockHandSprite.scrollFactor.y = 0;
			
			FlxG.state.add(clockFaceSprite);
			FlxG.state.add(nextClockFaceSprite);
			FlxG.state.add(clockHandSprite);
			
			timerText = new FlxText(FlxG.width - clockFaceSprite.width, clockFaceSprite.height, clockFaceSprite.width).setFormat("PowellAntique", 16, 0x778ea1, "center", 0x233e58);
			timerText.scrollFactor.x = timerText.scrollFactor.y = 0;
			
			_levelTimer = new FlxTimer(duration * 2, repeat);
			
			_levelTimer.play();
			
			_highestTimeValue = _levelTimer.duration;
		}
		
		private var minutes:uint = 1; //must start != 0 to prevent bug
		private var seconds:uint;
		
		private function displayTimer():void
		{
			time = (_levelTimer.duration - (_levelTimer.progress * _levelTimer.duration)) / 2;
			
			var displayTime:int = time;
			minutes = 0;
			
			while (displayTime >= 60)
			{
				minutes++;
				displayTime -= 60;
			}
			
			seconds = displayTime % 60;
			
			if ((displayTime == 0) && (minutes == 0))
			{
				_levelTimer.pause();
			}
			
			timerText.text = minutes + ":";
			if (seconds < 10)
			{
				timerText.text += "0";
			}
			timerText.text += seconds; 
		}
		
		public function setTimerValue(time:Number = 0):void
		{
			_levelTimer.setDuration(time);
			_levelTimer.play();
		}
		
		public function Update():void 
		{
			_levelTimer.update();
			
			updateClockHand();
			
			displayTimer();
			
			if (time > _highestTimeValue)
			{
				_highestTimeValue = time;
			}
		}
		
		public function pauseTimer():void
		{
			_levelTimer.pause();
		}
		
		public function isTimeLeft():Boolean
		{
			if (minutes == 0 && seconds == 0)
			{
				return false;
			}
			else 
			{
				return true;
			}
		}
		
		public function addToTimer(time:Number):void
		{
			_levelTimer.addToDuration(time);
		}
		
		private var _fadeInMultiplier:Number = 3;
		
		private var _threshold1:Number = 0.9166666667;
		private var _threshold2:Number = 0.8333333333;
		private var _threshold3:Number = 0.75;
		private var _threshold4:Number = 0.6666666667;
		private var _threshold5:Number = 0.5833333333;
		private var _threshold6:Number = 0.50;
		private var _threshold7:Number = 0.4166666667;
		private var _threshold8:Number = 0.3333333333;
		private var _threshold9:Number = 0.25;
		private var _threshold10:Number = 0.1666666667;
		private var _threshold11:Number = 0.0833333333;
		private var _threshold12:Number = 0.0;
		private var _thresholdDifference:Number = _threshold1 - _threshold2;
		
		private var _nextClockSetting:uint;
		
		private function updateClockHand():void
		{
			var ratioRemaining:Number = time / _highestTimeValue;
			clockHandSprite.angle = ratioRemaining * -360;
			
			ratioRemaining += _thresholdDifference / 1.2;
			
			if (ratioRemaining > _threshold1 + _thresholdDifference)
			{
				clockFaceSprite.play("zero");
				nextClockFaceSprite.play("zero");
			}
			else if (ratioRemaining > _threshold1)
			{
				clockFaceSprite.play("zero");
				nextClockFaceSprite.play("one");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold1) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold2)
			{
				clockFaceSprite.play("one");
				nextClockFaceSprite.play("two");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold2) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold3)
			{
				clockFaceSprite.play("two");
				nextClockFaceSprite.play("three");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold3) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold4)
			{
				clockFaceSprite.play("three");
				nextClockFaceSprite.play("four");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold4) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold5)
			{
				clockFaceSprite.play("four");
				nextClockFaceSprite.play("five");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold5) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold6)
			{
				clockFaceSprite.play("five");
				nextClockFaceSprite.play("six");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold6) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold7)
			{
				clockFaceSprite.play("six");
				nextClockFaceSprite.play("seven");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold7) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold8)
			{
				clockFaceSprite.play("seven");
				nextClockFaceSprite.play("eight");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold8) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold9)
			{
				clockFaceSprite.play("eight");
				nextClockFaceSprite.play("nine");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold9) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold10)
			{
				clockFaceSprite.play("nine");
				nextClockFaceSprite.play("ten");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold10) / _thresholdDifference);
			}
			else if (ratioRemaining > _threshold11)
			{
				clockFaceSprite.play("ten");
				nextClockFaceSprite.play("eleven");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold11) / _thresholdDifference);
			}
			else
			{
				clockFaceSprite.play("eleven");
				nextClockFaceSprite.play("twelve");
				nextClockFaceSprite.alpha = Math.abs(1 - (ratioRemaining - _threshold12) / _thresholdDifference);
			}
			
			nextClockFaceSprite.alpha *= _fadeInMultiplier;
			
		}
		
	}

}